package com.gct.controller;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.gct.controller.base.BaseController;
import com.gct.domain.Art;
import com.gct.domain.wrapper.DomainWrapper;
import com.gct.service.ArtService;
import com.gct.view.dto.ArtStatisticsDto;

/*
 * Newest Page
 */
@RestController
@RequestMapping("/art")

public class ArtController extends BaseController {
    @Autowired
    private ArtService artService;

    @RequestMapping(value = "/newestarts", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DomainWrapper<Art> queryArts() {
        try {
            DomainWrapper<Art> dmw = new DomainWrapper<Art>();
            dmw.setData(artService.getArts());
            // throw new RuntimeException("exception throwed");
            return dmw;
        } catch (Throwable e) {
            return this.<Art>processException(e);
        }
    }

    @RequestMapping(value = "/artdetails/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DomainWrapper<Art> queryArtDetails(@PathVariable("id") long id) {
        try {
            DomainWrapper<Art> dmw = new DomainWrapper<Art>();
            List<Art> arts = new LinkedList<Art>();
            arts.add(artService.getArtDetails(id));
            dmw.setData(arts);
            return dmw;
        } catch (Throwable e) {
            return this.<Art>processException(e);
        }
    }

    @RequestMapping(value = "/arturi/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DomainWrapper<Art> queryArtURIById(@PathVariable("id") long id) {
        try {
            DomainWrapper<Art> dmw = new DomainWrapper<Art>();
            List<Art> arts = new LinkedList<Art>();
            arts.add(artService.getArtURI(id));
            dmw.setData(arts);
            return dmw;
        } catch (Throwable e) {
            return this.<Art>processException(e);
        }
    }

    @RequestMapping(value = "/artmostplayed", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DomainWrapper<Art> queryArtsMostPlayed() {
        try {
            DomainWrapper<Art> dmw = new DomainWrapper<Art>();
            dmw.setData(artService.getArtsMostPlayed());
            return dmw;
        } catch (Throwable e) {
            return this.<Art>processException(e);
        }
    }

    @RequestMapping(value = "/artmostviewed", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DomainWrapper<Art> queryArtsMostViewed() {
        try {
            DomainWrapper<Art> dmw = new DomainWrapper<Art>();
            dmw.setData(artService.getArtsMostViewed());
            return dmw;
        } catch (Throwable e) {
            return this.<Art>processException(e);
        }
    }

    @RequestMapping(value = "/arthighlyvoted", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DomainWrapper<Art> queryArtsHighlyVoted() {
        try {
            DomainWrapper<Art> dmw = new DomainWrapper<Art>();
            dmw.setData(artService.getArtsHighlyVoted());
            return dmw;
        } catch (Throwable e) {
            return this.<Art>processException(e);
        }
    }


    @RequestMapping(value = "/artstatics", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DomainWrapper<Art> saveArtsStatics(@RequestBody List<ArtStatisticsDto> statisList) {
        try {
            if(null == statisList || statisList.size() == 0)
                return new DomainWrapper<Art>();

            LinkedList<ArtStatisticsDto> list = new LinkedList<ArtStatisticsDto>();
            list.addAll(new HashSet<ArtStatisticsDto>(statisList));
            if(statisList.size() > list.size())
                throw new RuntimeException("duplicate records received from client...");
//            artService.saveArtStatics(statisList);
            DomainWrapper<Art> dmw = new DomainWrapper<Art>();
            return dmw;
        } catch (Throwable e) {
            return this.<Art>processException(e);
        }
    }
}
